﻿using Libs;
using Libs.Egucha;
using System.Diagnostics;

namespace WriteForFighting.Game.View.Control
{
    delegate void ButtonEventHandler();

    abstract class ButtonBase : ControlBase
    {
        public event ButtonEventHandler OnButtonClicked;
        public int CollisionBoxWidth { get; protected set; }
        public int CollisionBoxHeight { get; protected set; }
        public bool Enabled { get; set; }

        protected bool IsMouseOver { get; private set; }
        protected int MouseOverTickCount { get; private set; }
        protected int MouseDownTickCount { get; private set; }
        protected bool IsMouseDownedOnThis { get; private set; }

#if DEBUG
        private static int colorCollisionBox = DX.GetColor(0, 255, 0);
#endif

        public ButtonBase(int par1X, int par2Y,
            int par3CollisionBoxWidth, int par4CollisionBoxHeigh)
            : base(par1X, par2Y)
        {
            CollisionBoxWidth = par3CollisionBoxWidth;
            CollisionBoxHeight = par4CollisionBoxHeigh;
            Enabled = true;
        }

        public override void Update()
        {
            if (!Enabled) { return; }

            int mx;
            int my;
            DX.GetMousePoint(out mx, out my);

            IsMouseOver = Collision.GetIsHitRectAndPoint(new Rect(new PointD(X, Y), CollisionBoxWidth, CollisionBoxHeight),
                                               new PointD(mx, my));
            if (IsMouseOver)
            {
                MouseOverTickCount++;
            }
            else
            {
                MouseOverTickCount = 0;
            }

            if ((DX.GetMouseInput() & DX.MOUSE_INPUT_LEFT) != 0)
            {
                if ((MouseDownTickCount == 0) && IsMouseOver)
                {
                    IsMouseDownedOnThis = true;
                }
                else if (!IsMouseDownedOnThis)
                {
                    IsMouseOver = false;
                }

                MouseDownTickCount++;
            }
            else if ((0 < MouseDownTickCount) && IsMouseDownedOnThis && IsMouseOver)
            {
                MouseDownTickCount = 0;
                IsMouseDownedOnThis = false;
                OnButtonClicked();
            }
            else
            {
                MouseDownTickCount = 0;
                IsMouseDownedOnThis = false;
            }
            
        }

        public override void Draw()
        {
            if ((0 < MouseDownTickCount) && IsMouseDownedOnThis)
            {
                const int bright = 220;
                DX.SetDrawBright(bright, bright, bright);
            }
            else if (IsMouseOver)
            {
                int alpha = 180;
                if (MouseOverTickCount * 3 < (255 - alpha))
                {
                    alpha = 255 - MouseOverTickCount * 3;
                }

                DX.SetDrawBlendMode(DX.DX_BLENDMODE_ALPHA, alpha);
            }

            if (!Enabled)
            {
                const int bright = 120;
                DX.SetDrawBright(bright, bright, bright);
            }

            OnDraw();

            DX.SetDrawBlendMode(DX.DX_BLENDMODE_NOBLEND, 0);
            DX.SetDrawBright(255, 255, 255);

#if DEBUG
            DrawDataForDebug();
#endif
        }

        protected virtual void OnDraw() { }

#if DEBUG
        private void DrawDataForDebug()
        {
            DX.SetDrawBlendMode(DX.DX_BLENDMODE_ALPHA, 200);
            DX.DrawBox(X, Y, X + CollisionBoxWidth, Y + CollisionBoxHeight, colorCollisionBox, DX.FALSE);
            DX.SetDrawBlendMode(DX.DX_BLENDMODE_NOBLEND, 255);
        }
#endif
    }
}
